package com.guet.ltomanagement.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guet.ltomanagement.common.entities.ResultBody;
import com.guet.ltomanagement.common.entities.SysUser;
import com.guet.ltomanagement.entity.SysUnit;
import com.guet.ltomanagement.mapper.SysUnitMapper;
import com.guet.ltomanagement.service.SysUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjj
 * @since 2023-03-03
 */
@Api(tags = "单位接口")
@RestController
@RequestMapping("/sys/unit")
public class SysUnitController extends BaseController {
    @Autowired
    private SysUnitService sysUnitService;

    @ApiOperation(value = "添加单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUnit", value = "单位类", required = true, dataType = "SysUnit"),
    })
    @PostMapping("/add")
    public ResultBody add(@Validated @RequestBody SysUnit sysUnit)
    {
        return ResultBody.success(sysUnitService.add(sysUnit));
    }

    @ApiOperation(value = "更新单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUnit", value = "单位类", required = true, dataType = "SysUnit"),
    })
    @PostMapping("/update")
    public ResultBody update(@Validated @RequestBody SysUnit sysUnit)
    {
        return ResultBody.success(sysUnitService.update(sysUnit));
    }

    @ApiOperation(value = "删除单位")
    @ApiImplicitParam(name = "sysUnitId", value = "单位ID", required = true, dataType = "Integer")
    @DeleteMapping("/delete")
    public ResultBody delete(@RequestParam("id") Integer sysUnitId)
    {
        return ResultBody.success(sysUnitService.delete(sysUnitId));
    }

    @ApiOperation(value = "分页获取单位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页元素个数", required = true, dataType = "Integer")
    })
    @GetMapping("/list/page")
    public ResultBody getUnitListPage(@RequestParam Integer page, @RequestParam Integer size)
    {
        return ResultBody.success(sysUnitService.getUnitListPage(page, size));
    }

    @ApiOperation(value = "根据Id获取单元数据")
    @ApiImplicitParam(name = "Id", value = "id", required = true, dataType = "Integer")
    @GetMapping("/id")
    public ResultBody getUnitById(@RequestParam Integer id)
    {
        return ResultBody.success(sysUnitService.getUnitById(id));
    }

    @ApiOperation(value = "获取单元的树形结构")
    @GetMapping("/tree")
    public ResultBody getUnitTree()
    {
        return ResultBody.success(sysUnitService.getUnitTree());
    }

    @ApiOperation(value = "获取单位列表")
    @GetMapping("/list")
    public ResultBody getUnitList()
    {
        return ResultBody.success(sysUnitService.getUnitList());
    }
}
